package com.chenfan.ccp.common.result;


import lombok.*;

import java.io.Serializable;

/**
 * @className: R
 * @description: 返回结果集
 * @author: Leo
 * @date: 2020/12/27 8:11 上午
 **/

@Data
@Getter
@Setter
@ToString
@NoArgsConstructor
public class R<T> implements Serializable {


	public static int SUCCESS = 0;
	public static int FAIL = -1;

	public static final String SUCCESS_MESSAGE = "操作成功";

	/**
	 * 默认失败消息
	 */
	public static final String FAILURE_MESSAGE = "业务异常";

	private static final long serialVersionUID = 1L;

	private int code;

	private T obj;

	private String message;

	private R(IResultCode resultCode) {
		this(resultCode, null, resultCode.getMessage());
	}

	private R(IResultCode resultCode, String msg) {
		this(resultCode, null, msg);
	}

	private R(IResultCode resultCode, T data) {
		this(resultCode, data, resultCode.getMessage());
	}

	private R(IResultCode resultCode, T data, String msg) {
		this(resultCode.getCode(), data, msg);
	}

	private R(int code, String message) {
		this.code = code;
		this.message = message;
	}


	private R(int code, T obj, String message) {
		this.code = code;
		this.obj = obj;
		this.message = message;
	}

	/**
	 * 返回R
	 *
	 * @param <T> T 泛型标记
	 * @return R
	 */
	public static <T> R<T> ok() {
		return new R<>(R.SUCCESS, SUCCESS_MESSAGE);
	}

	/**
	 * 返回R
	 *
	 * @param msg 消息
	 * @param <T> T 泛型标记
	 * @return R
	 */
	public static <T> R<T> ok(String msg) {
		return new R<>(R.SUCCESS , msg);
	}

	/**
	 * 返回R
	 *
	 * @param data 数据
	 * @param <T>  T 泛型标记
	 * @return R
	 */
	public static <T> R<T> ok(T data) {
		return data(data, SUCCESS_MESSAGE);
	}

	/**
	 * 返回R
	 *
	 * @param data 数据
	 * @param <T>  T 泛型标记
	 * @return R
	 */
	public static <T> R<T> data(T data) {
		return data(data, SUCCESS_MESSAGE);
	}

	/**
	 * 返回R
	 *
	 * @param data 数据
	 * @param msg  消息
	 * @param <T>  T 泛型标记
	 * @return R
	 */
	public static <T> R<T> data(T data, String msg) {
		return data(R.SUCCESS, data, msg);
	}

	/**
	 * 返回R
	 *
	 * @param code 状态码
	 * @param data 数据
	 * @param msg  消息
	 * @param <T>  T 泛型标记
	 * @return R
	 */
	public static <T> R<T> data(int code, T data, String msg) {
		return new R<>(code, data, data == null ? SUCCESS_MESSAGE : msg);
	}



	/**
	 * 返回R
	 *
	 * @param resultCode 业务代码
	 * @param <T>        T 泛型标记
	 * @return R
	 */
	public static <T> R<T> ok(IResultCode resultCode) {
		return new R<>(resultCode.getCode(),resultCode.getMessage());
	}

	/**
	 * 返回R
	 *
	 * @param <T> T 泛型标记
	 * @return R
	 */
	public static <T> R<T> fail() {
		return new R<>(R.FAIL, FAILURE_MESSAGE);
	}

	/**
	 * 返回R
	 *
	 * @param msg 消息
	 * @param <T> T 泛型标记
	 * @return R
	 */
	public static <T> R<T> fail(String msg) {
		return new R<>(R.FAIL,null, msg);
	}


	/**
	 * 返回R
	 *
	 * @param code 状态码
	 * @param msg  消息
	 * @param <T>  T 泛型标记
	 * @return R
	 */
	public static <T> R<T> fail(int code, String msg) {
		return new R<>(code, null, msg);
	}

	/**
	 * 返回R
	 *
	 * @param resultCode 业务代码
	 * @param <T>        T 泛型标记
	 * @return R
	 */
	public static <T> R<T> fail(IResultCode resultCode) {
		return new R<>(resultCode.getCode(), resultCode.getMessage());
	}

	/**
	 * 返回R
	 *
	 * @param flag 成功状态
	 * @return R
	 */
	public static <T> R<T> status(boolean flag) {
		return flag ? ok(SUCCESS_MESSAGE) : fail(FAILURE_MESSAGE);
	}


}
